﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using COFE.Core.Utils;
using COFE.Core.Validater;
using COFE.Core.Filter;
using COFE.Core;
using System.Runtime.InteropServices;
using QuickZip.Converters;

namespace COFE.Core.EntryType
{
    /// <summary>
    /// This IconProvider uses ShellImageList (which uses Win32API) to return an icon.
    /// </summary>
    public class ShellIconProvider : IEntryTypeIconProvider
    {
        public static ShellIconProvider Instance = new ShellIconProvider();


        #region Constructor
        public ShellIconProvider()
        {
        }
        #endregion

        #region Methods


        public byte[] GetIconStream(IEntryFilter filter, IconProperties size)
        {
            Func<IconProperties, IconSize> convertSize = (s) =>
                {
                    switch (s)
                    {
                        case IconProperties.ExtraLargeIcon: return IconSize.extraLarge;
                        case IconProperties.JumboIcon: return IconSize.jumbo;
                        case IconProperties.LargeIcon: return IconSize.large;
                        case IconProperties.SmallIcon: return IconSize.small;                        
                    }
                    return IconSize.small;
                };

            Bitmap retVal = null;
            if (filter != null)
            {
                if (filter is FileTypeEntryFilter)
                {
                    string ext = (filter as FileTypeEntryFilter).Value;

                    using (var imgList = new SystemImageList(convertSize(size)))
                    {
                        if (ext == "folder")
                            retVal = imgList["", true, false];
                        else
                        {
                            retVal = imgList["AAA." + ext, false, false];
                        }
                    }

                }
            }

            retVal.MakeTransparent();
            return retVal.EnsureBitmapSize(size).ConvertToBitmapArray();
        }


        #endregion

        #region Data


        #endregion

        #region Public Properties

        #endregion



    }
}
